<template>
  <a-modal
    title="增加船舶离港记录"
    :width="600"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!--      经 Form.create() 包装过的组件会自带 this.form 属性，如果使用 template 语法，可以使用-->
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="离港时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-date-picker @change="onChange"
                             @ok="getDataValue"
                             :show-time="{ format: 'HH:mm' }"
                             format="YYYY-MM-DD HH:mm"
                             v-decorator="['departureTime']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>

  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { leaveShip } from '@/api/ship/ship'

  export default {
    props: {

    },
      data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        ship: {},

        form: this.$form.createForm(this)
      }
    },
    methods: {
      leave (record) {
        this.visible = true
        this.ship = record
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          // console.log(values)
          // console.log(values.departureTime)
          if (values.departureTime !== undefined) {
            values.departureTime = moment(values.departureTime).format('YYYY-MM-DD HH:mm:ss')
          }
          if (!errors) {
            leaveShip(Object.assign(values, { 'shipId': this.ship.shipId }))
              .then(() => {
                this.confirmLoading = false
                this.visible = false
                this.form.resetFields()
                this.$emit('ok')
              }).catch(err => {
              this.confirmLoading = true
              this.$message.error(err.result || '操作失败')
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChange (date, dateString) {
        const data = this.$moment(date).format('YYYY/MM/DD HH:mm:ss')
        this.depatureTime = data
      },
      getDataValue (value) {
        value = moment(value).format('YYYY-MM-DD HH:mm:ss') // 转换
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
